<template>
  <div>
    <el-row :gutter="10">
      <el-col :span="4">
        <el-select v-model="selectType" value="">
          <el-option v-for="item in type" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="selectPostType" value="" placeholder="帖子类型">
          <el-option label="" value=""></el-option>
          <el-option label="寻物" :value="0"></el-option>
          <el-option label="认领" :value="1"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="selectItemType" value="" placeholder="物品类型">
          <el-option label="" value=""></el-option>
          <el-option v-for="item in itemTypeList" :label="item.type" :value="item.id"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4" style="margin-right: 10px">
        <el-select v-model="selectReivewStatus" placeholder="审核状态" value="">
          <el-option label="" value=""></el-option>
          <el-option label="未审核" value="0"></el-option>
          <el-option label="已通过" value="2"></el-option>
          <el-option label="未通过" value="1"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="selectCompleteType" value="" placeholder="完成状态">
          <el-option label="" value=""></el-option>
          <el-option label="未完成" :value="0"></el-option>
          <el-option label="已完成" :value="1"></el-option>
        </el-select>
      </el-col>
    </el-row>
    <el-row style="margin-top: 1%;">
      <el-col :span="6">
        <el-input v-model="keyword" placeholder="输入关键字"></el-input>
      </el-col>
      <el-col span="auto" style="margin-left: 1%;">
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-col>
    </el-row>
    <el-empty description="这里什么也没有（；´д｀）ゞ" v-show="!postList.length"></el-empty>
    <MyPost :post="item" v-for="item in postList" :key="new Date().getTime()"></MyPost>
    <!-- 底部分页 -->
    <div style="text-align: center">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="currentPage" :page-sizes="[5, 10, 15]" :page-size="pageSize" :total="totalSize" background
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import MyPost from "@/views/home/mine/components/MyPost.vue";
import { getPostListByMultipleCondition } from "@/api/search/post";
import { getItemTypeList } from "@/api/post/itemType";
import { getUserStarIds } from "@/api/post/postStar";

export default {
  components: { MyPost },
  data() {
    return {
      user: {},
      postList: [],
      currentPage: 1,
      pageSize: 5,
      totalSize: 0,
      itemTypeList: [],
      type: [{
        label: '我的帖子',
        value: 0
      }, {
        label: '我完成的',
        value: 1
      }, {
        label: '我的收藏',
        value: 2
      }],
      selectType: 0,
      selectPostType: '',
      selectItemType: '',
      selectCompleteType: '',
      selectReivewStatus: '',
      keyword: '',
      starIds: ''
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.getPostList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getPostList()
    },
    search() {
      this.getPostList()
    },
    getItemTypeList() {
      getItemTypeList().then(res => {
        if (res.data.code === 200) {
          this.itemTypeList = res.data.itemTypeInfoList
        } else {
          // this.$message.error(res.data.message)
        }
      })
    },
    getPostList() {
      let data = {
        type: this.selectPostType === 2 ? undefined : this.selectPostType,
        itemTypeId: this.selectItemType === 0 ? undefined : this.selectItemType,
        completeStatus: this.selectCompleteType === 2 ? undefined : this.selectCompleteType,
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword === '' ? undefined : this.keyword,
        reviewStatus: this.selectReivewStatus === '' ? undefined : this.selectReivewStatus,
        sortByGmtCreate: 'desc'
      }
      if (this.selectType === 0) {
        this.$set(data, "fromUserId", this.user.id)
      } else if (this.selectType === 1) {
        this.$set(data, "toUserId", this.user.id)
      } else if (this.selectType === 2) {
        if (this.starIds === '') {
          this.postList = []
          this.totalSize = 0
          return
        } else {
          this.$set(data, "postIds", this.starIds)
        }
      }
      getPostListByMultipleCondition(data).then(res => {
        if (res.data.code === 200) {
          if (res.data.searchResult.dataList !== null) {
            this.postList = res.data.searchResult.dataList
            this.totalSize = res.data.searchResult.totalCount
          } else {
            this.postList = []
            this.totalSize = 0
          }
        } else {
          // this.$message.error(res.data.message)
        }
      })
    }
  },
  created() {
    this.user = JSON.parse(Cookies.get("user"))
    this.getItemTypeList()
    this.getPostList()
  },
  watch: {
    keyword() {
      this.getPostList()
    },
    selectType(newVal) {
      if (newVal === 2) {
        getUserStarIds({
          userId: this.user.id
        }).then(res => {
          if (res.data.code === 200) {
            if (res.data.postIds !== null) {
              let len = res.data.postIds.length
              res.data.postIds.forEach((item, idx) => {
                let id = JSON.stringify(item)
                if (idx < len - 1) {
                  id += ','
                }
                this.starIds += id
              })
            } else {
              this.starIds = ''
            }
          } else {
            // this.$message.error(res.data.message)
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.notice {
  font-size: 17px;
  font-weight: bold;
  margin-top: 0;
  margin-bottom: 0;
}
</style>
